package initdb

import (
	baseAuth "gitee.com/zaiqiang231/go-base-app/base_app/auth"
	baseLog "gitee.com/zaiqiang231/go-base-app/base_app/log"
	"gitee.com/zaiqiang231/go-base-app/base_app/store"
	baseStore "gitee.com/zaiqiang231/go-base-app/base_app/store"
	baseInitdb "gitee.com/zaiqiang231/go-base-app/base_app/store/initdb"
	"gitee.com/zaiqiang231/zcamp-service-user/app/model"
)

func RegisterInit() {
	store.RegisterInit(baseInitdb.DbTable[model.UserIdSuper]{})
	store.RegisterInit(initUser{})
	store.RegisterInit(baseInitdb.DbTable[model.UserAdmin]{})
	store.RegisterInit(baseInitdb.DbTable[model.Follow]{})
	store.RegisterInit(baseInitdb.DbTable[model.Fans]{})

	store.GetDataStore().CreateTables()
}

type initUser struct {
	baseInitdb.DbTable[model.User]
}

func (i initUser) GetTable() baseStore.TableInterface {
	return model.User{}
}

func (i initUser) InitData(ds *baseStore.Datastore) {
	password, err := baseAuth.Encrypt("123456")
	if err != nil {
		baseLog.Errorf("initUser InitData fail, %v", err)
		return
	}
	var user = model.User{Name: "admin", Password: password, Uid: 100000}
	err = ds.DB.Create(&user).Error
	if err != nil {
		baseLog.Errorf("initUser InitData Create fail, %v", err)
		return
	}
}
